package DDZ;

import java.util.*;

public class DouDiZhu {
    //游戏开始
    public static void start() {

        while (true) {
            //用户选择界面
            userSelectionInterface();

            //游戏结束界面
            gameEndInterface();
        }

    }
    //游戏开始界面
    private static void gameStartInterface() {
        //界面
        System.out.println("-------------------简化斗地主-------------------");
        System.out.println("----------------请选择是否进行游戏-----------------");
        System.out.println("                1.是                            ");
        System.out.println("                2.否                            ");

    }

    /**
     * 用户选择界面
     */
    private static void userSelectionInterface() {
        //输入开启
        Scanner scanner = new Scanner(System.in);
        //sc接收字符
        int sc=1;
        while (true) {
            try {
                gameStartInterface();
                sc = scanner.nextInt();
                switch (sc) {
                    case 1:
                        gameProgress();
                        return;
                    case 2:
                        return;
                    default:
                        System.out.println("输入错误，请重新输入 1 或 2");
                }
            } catch (InputMismatchException e) {
                System.out.println("输入格式错误，请输入有效的整数。");
                scanner.nextLine(); // 消耗掉错误的输入
            }
        }

    }

    //游戏结束界面
    private static void gameEndInterface() {
        System.out.println("----------------游戏结束-----------------");
        System.out.println("----------------是否继续-----------------");
    }


    //游戏进行
    /**
     * 分析业务需求
     总共有 54 张牌
     点数: "3","4","5","6","7","8","9","10","J","Q","K","A","2"
     花色: "♠", "♥", "♣", "♦"
     大小王: "🙎‍♂️", "🎨"
     斗地主：发出 51 张牌，剩下 3 张做为底牌。
     分析实现
     在启动游戏房间的时候，应该提前准备好 54 张牌
     接着，需要完成洗牌、发牌、对牌排序、看牌
     */
    private static void gameProgress() {
        //初始化牌库和玩家
        List<String> initializationPoker=new ArrayList<>();
        List<String> play1Poker=new ArrayList<>();
        List<String> play2Poker=new ArrayList<>();
        List<String> play3Poker=new ArrayList<>();
        List<String> holePoker=new ArrayList<>();
        initializationPoker(initializationPoker);
        //洗牌
        shufflePoker(initializationPoker);
        //发牌
        distributePoker(play1Poker,play2Poker,play3Poker,holePoker,initializationPoker);
        //排序
        sortPoker(play1Poker);
        sortPoker(play2Poker);
        sortPoker(play3Poker);
        //看牌
        lookPoker(play1Poker);
        lookPoker(play2Poker);
        lookPoker(play3Poker);
        lookPoker(holePoker);

    }

    /**
     * 初始化牌库
     * @param initializationPoker 空牌库
     */
    private static void initializationPoker(List<String> initializationPoker) {
        String[] point={"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
        String[] pokerSuit ={"♠", "♥", "♣", "♦"};
        String[] joker ={"🙎‍♂️", "🎨"};
        for (String value : pokerSuit) {
            for (String s : point) {
                initializationPoker.add(value + s);
            }
        }
        initializationPoker.add(joker[0]);
        initializationPoker.add(joker[1]);
    }

    /**
     * 洗牌
     * @param initializationPoker 有序牌库
     */
    private static void shufflePoker(List<String> initializationPoker) {
        //随机数对象
        Random random = new Random();
        int n=initializationPoker.size(),j=0;
        String temp=null;
        for (int i = n-1; i > 0; i--) {
            j=random.nextInt(i);
            temp=initializationPoker.get(i);
            initializationPoker.set(i,initializationPoker.get(j));
            initializationPoker.set(j,temp);
        }
    }


    /**
     * 发牌
     * @param play1 玩家1的牌
     * @param play2 玩家2的牌
     * @param play3 玩家3的牌
     * @param holeCards 底牌
     * @param initializationPoker 洗乱的牌库
     */
    private static void distributePoker(List<String> play1, List<String> play2, List<String> play3, List<String> holeCards, List<String> initializationPoker) {
        for (int i = 0; i < initializationPoker.size(); i++) {
            if(i<17){
                play1.add(initializationPoker.get(i));
            } else if(i<34){
                play2.add(initializationPoker.get(i));
            }else if(i<51){
                play3.add(initializationPoker.get(i));
            }else {
                holeCards.add(initializationPoker.get(i));
            }
        }

    }

    /**
     * 看牌
     * @param playPoker 玩家手牌
     */
    private static void lookPoker(List<String> playPoker) {
        System.out.print("您的手牌是");
        System.out.println(playPoker);
    }

    private static void sortPoker(List<String> playPoker) {
        // 定义点数顺序
        String pointOrder = "345678910JQKA2";
        // 定义花色顺序
        String suitOrder = "♠♥♣♦";
        //定义大小王
        String jokerOrder = "🙎‍♂️🎨";
        //自定义比较器
        Comparator<String> pokerCompare= (card1,card2)->{
            //提起第一张牌的点数
            String card1Point=card1.substring(1);
            //提起第一张牌的花数
            String card1Suit=card1.substring(0,1);
            //提起第二张牌的点数
            String card2Point=card2.substring(1);
            //提起第二张牌的花数
            String card2Suit=card2.substring(0,1);
            //比较大小王
            if(jokerOrder.contains(card1)){
                if(jokerOrder.contains(card2)){
                    return jokerOrder.indexOf(card2Point)-jokerOrder.indexOf(card1Point);
                }
                return -1;
            }if(jokerOrder.contains(card2)){
                return 1;
            }

            //比较点数
            int point1Index=pointOrder.indexOf(card1Point);
            int point2Index=pointOrder.indexOf(card2Point);
            if(point1Index!=point2Index) {
                return   point2Index-point1Index;
            }

            int suit1Index=suitOrder .indexOf(card1Suit);
            int suit2Index=suitOrder .indexOf(card2Suit);
            return suit2Index-suit1Index;
        };
        Collections.sort(playPoker,pokerCompare);
    }
}
